package com.mall.bargain.controller;

import com.github.pagehelper.PageInfo;
import com.mall.bargain.fenign.ItemFeignService;
import com.mall.bargain.service.GoodsService;
import com.mall.bargain.service.IBargainService;
import com.mall.bargain.vo.VoBargain;
import com.mall.bargain.vo.VoTbGoods;
import com.mall.bargain.vo.VoTbImage;
import com.mall.common.response.CommonResponse;
import com.mall.entity.TbBargain;
import com.mall.entity.TbItmeSpec;
import com.mall.entity.bargain.BgBargain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author: W
 * @date: 2021/07/20/17:16
 * Other: 拼团活动
 */
@RestController
@RequestMapping("/bargain")
@CrossOrigin
public class BargainController {

    @Autowired
    private IBargainService bargainService;

    @Autowired
    private GoodsService goodsService;


    /**
     * @author Administrator
     * 查询商品列表
     * */
    @PostMapping("/findGoodsAll")
    private PageInfo findGoodsAll(@RequestBody VoTbGoods goods,
                                  @RequestParam(value = "page",defaultValue = "1") int page,
                                  @RequestParam(value = "size",defaultValue = "2") int size){
        return goodsService.findGoodsAll(goods,page,size);
    }

    /**
     * @author Administrator
     * 回显商品主图和商品轮播图
     * */
    @GetMapping("/findImage")
    public VoTbImage findImage(Long id){
        return bargainService.findImage(id);
    }

    /**
     * @author Administrator
     * 添加砍价商品
     * */
    @PostMapping("/addBargain")
    private CommonResponse addBargain(@RequestBody VoBargain bargain){
        try{
            bargainService.addBargain(bargain);
            return new CommonResponse(0,"success");
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(-1,"false");
        }
    }

    /**
     * @author Administrator
     * 砍价商品列表
     * */
    @PostMapping("/findBargainAll")
    public PageInfo findBargainAll(@RequestBody VoBargain bargain,
                                   @RequestParam(value = "page",defaultValue = "1") int page,
                                   @RequestParam(value = "size",defaultValue = "2") int size){
        return bargainService.findBargainAll(bargain,page,size);
    }

    /**
     * @author Administrator
     * 修改商品列表
     * */
    @PutMapping("/updateBargainById")
    public CommonResponse updateBargainById(@RequestBody TbBargain bargain){
        try{
            bargainService.updateBargainById(bargain);
            return new CommonResponse(0,"success");
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(-1,"false");
        }
    }

    /**
     * @author Administrator
     * 回显商品列表
     * */
    @GetMapping("/findBargainOne")
    public TbBargain findBargainOne(Long id){
        return bargainService.findBargainOne(id);
    }

    /**
     * @author Administrator
     * 逻辑删除商品列表
     * */
    @DeleteMapping("/delBargainOne")
    public CommonResponse delBargainOne(Integer id){
        try{
            bargainService.delBargainOne(id);
            return new CommonResponse(0,"success");
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(-1,"false");
        }
    }

    /**
     * @author Administrator
     * 逻辑删除商品列表
     * */
    @GetMapping("/findItemSpecAll")
    public List<TbItmeSpec> findItemSpecAll(Long goodsId){
        return bargainService.findItemSpecAll(goodsId);
    }

    /**
     * 获取可砍价商品列表
     */
    @GetMapping("/findList")
    public List<BgBargain> findList() {
        return bargainService.findList();
    }


    /**
     * 开始砍价
     *
     * @param uid uid 用户id
     * @param id  id   砍价商品id
     * @return boolean
     */
    @GetMapping("/startBargain")
    public CommonResponse startBargain(Integer uid, Long id) {
        return bargainService.startBargain(uid, id);
    }

    /**
     * 好友帮忙砍一刀
     */
    @GetMapping("/cutAKnife")
    public CommonResponse cutAKnife(Integer uid, Integer userBargainId) {
        return bargainService.cutAKnife(uid, userBargainId);
    }

    @PutMapping("/updateStatusById")
    public CommonResponse updateStatusById(
            @RequestParam("id") Long id,
            @RequestParam("status") String status
    ){
        return bargainService.updateStatusById(id,status);
    }

}
